/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import connectDB.ConnectDB;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author gm_ba_000
 */
public class testA04 extends javax.swing.JFrame {

    private ConnectDB conn4;
    private DefaultTableModel tableModel;
    private Date date;
    private String EmpCode;

    public testA04(ConnectDB conn, Date date, String EmpCode) {
        initComponents();
        setLocationRelativeTo(null);
        conn4 = conn;
        tableModel = new DefaultTableModel();
        this.date = date;
        this.EmpCode = EmpCode;
        loadData();

    }

    private void loadData() {
        // Load Column name
        Object[] colName = {"", "Day", "Working Class", "Start Class", "End Class", "Start Time", "EndTime"};
        tableModel.setColumnIdentifiers(colName);

        DateFormat formatDate = new SimpleDateFormat("yyyyMMdd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH);
        int DoM = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        int day = 1;

        ArrayList<String> arrRow = new ArrayList<String>();
        try {
            ResultSet rs = null;
            String query = "Select WRK_KBN, WRK_GO_KBN, WRK_LEFT_KBN,"
                    + "WRK_START_TIME, WRK_END_TIME, CAL_YMD From WORKTIME WRK "
                    + "where WRK.EMP_CODE = '" + EmpCode + "'";
            rs = conn4.excuteQuery(query);

            for (int i = 1; i <= DoM; i++) {
                
                for (int col = 1; col <= 7; col++) {
                    cal.set(year, month, i);
                    int DoW = cal.get(Calendar.DAY_OF_WEEK);    // Lấy ngày trong tuần với ngày i
                    String today = formatDate.format(cal.getTime());    // Lấy yyyyMMdd hiện tại
                    //System.out.println("today " + col + ": " + today);
                    boolean haveRecord = false;

                    //Column 1
                    if (col == 1) {
                        arrRow.add("" + day);
                        day++;
                    }

                    //Column 2
                    if (col == 2) {
                        // Gán tên ngày của tuần
                        String strDoW = checkDayOfWeek(DoW);
                        arrRow.add(strDoW);

                    }
                    
                    if (col == 3) {
                        String WRK_KBN = checkRecord(rs, today, "WRK_KBN");

                        if (WRK_KBN.trim().equals("01")) {
                            arrRow.add("Normal");
                        } else if (WRK_KBN.trim().equals("02")) {
                            arrRow.add("Absent");
                        } else if (WRK_KBN.trim().equals("03")){
                            arrRow.add("Holiday");
                        } else {
                            arrRow.add(" ");
                        }
                    }

                    if (col == 4) {
                        arrRow.add(checkRecord(rs, today, "WRK_GO_KBN"));
                    }

                    if (col == 5) {
                        arrRow.add(checkRecord(rs, today, "WRK_LEFT_KBN"));
                    }

                    if (col == 6) {
                        arrRow.add(checkRecord(rs, today, "WRK_START_TIME"));
                    }

                    if (col == 7) {
                        arrRow.add(checkRecord(rs, today, "WRK_END_TIME"));
                    }
                } // End for Column
                tableModel.addRow(arrRow.toArray());
                arrRow.clear();
            } //End for Row
            tblData.setModel(tableModel);
        } catch (Exception e) {
            System.out.println("Wrong in result set");
        }

    }

    public String checkRecord(ResultSet rs, String today, String colName) {
        try {
            boolean haveRecord = false;
            while (rs.next()) {
                if (rs.getString("CAL_YMD").trim().equals(today)) {
                    haveRecord = true;
                    //Column 4

                    return rs.getString(colName).trim();
                }
                System.out.println("Value in database: " + rs.getString(colName));
            }
            if (!haveRecord) {
                return " ";
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return "";
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblData = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        btBack = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tblData.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(tblData);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel1.setText("A04");

        btBack.setText("Back A02");
        btBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btBackActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 597, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(279, 279, 279)
                                .addComponent(jLabel1))
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(btBack)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 36, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btBack)
                .addGap(36, 36, 36))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btBackActionPerformed
        // TODO add your handling code here:
        frm_STAFF_A02 frmA02 = new frm_STAFF_A02(EmpCode, conn4);
        frmA02.setVisible(true);
        setVisible(false);
    }//GEN-LAST:event_btBackActionPerformed
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btBack;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblData;
    // End of variables declaration//GEN-END:variables

    private String checkDayOfWeek(int day) {
        if (day == 1) {
            return "Sun";
        } else if (day == 2) {
            return "Mon";
        } else if (day == 3) {
            return "Tue";
        } else if (day == 4) {
            return "Wed";
        } else if (day == 5) {
            return "Fri";
        } else if (day == 6) {
            return "Thu";
        } else {
            return "Sat";
        }
    }
}
